package com.indexingsystem.boss.token;
//package com.indexingsystem.boss.token;
//
//import java.util.Date;
//import java.util.Set;
//
//import javax.annotation.Resource;
//
//import org.apache.commons.lang.StringUtils;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.AccountException;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.AuthenticationInfo;
//import org.apache.shiro.authc.AuthenticationToken;
//import org.apache.shiro.authc.DisabledAccountException;
//import org.apache.shiro.authc.SimpleAuthenticationInfo;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.apache.shiro.subject.SimplePrincipalCollection;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import com.indexingsystem.boss.Vo.PUserPwVo;
//import com.indexingsystem.boss.entity.Pusers;
//import com.indexingsystem.boss.token.manager.TokenManager;
//import com.indexingsystem.boss.utils.DateUtil;
//import com.indexingsystem.system.service.IOperationsService;
//import com.indexingsystem.system.service.IPusersService;
//import com.indexingsystem.system.service.IRolesService;
//
////import com.sojson.common.model.UUser;
////import com.sojson.core.shiro.token.manager.TokenManager;
////import com.sojson.permission.service.PermissionService;
////import com.sojson.permission.service.RoleService;
////import com.sojson.user.service.UUserService;
//
///**
// * shiro 认证 + 授权 重写.
// * 
// * @author Administrator
// *
// */
//public class LoginRealm extends AuthorizingRealm {
//
//	@Resource
//	private IPusersService iPusersService;
//
//	// @Autowired
//	// PermissionService permissionService;
//
//	@Resource
//	private IOperationsService iOperationsService;
//
//	@Resource
//	private IRolesService iRolesService;
//
//	// @Autowired
//	// RoleService roleService;
//
//	public LoginRealm() {
//		super();
//	}
//
//	/**
//	 * 认证信息，主要针对用户登录，
//	 */
//	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
//			throws AuthenticationException {
//
//		ShiroToken token = (ShiroToken) authcToken;
//		PUserPwVo puserPwVo =new PUserPwVo();
//    	puserPwVo.setPassword(token.getPswd());
//    	puserPwVo.setUserName(token.getUsername());
//        Pusers user = iPusersService.login(puserPwVo);
//		if (null == user) {
//			throw new AccountException("帐号或密码不正确！");
//			/**
//			 * 如果用户的status为禁用。那么就抛出<code>DisabledAccountException</code>
//			 */
//		} else if (Pusers._0.equals(user.getStatus())) {
//			throw new DisabledAccountException("帐号已经禁止登录！");
//		} else {
//			// 更新登录时间 last login time
//			user.setLastLoginDate(DateUtil.parseFormatToDate(DateUtil.getCurTotalDate()));
//			iPusersService.updateByPrimaryKeySelective(user);
//		}
//		return new SimpleAuthenticationInfo(user, user.getPassword(), getName());
//	}
//
//	/**
//	 * 授权
//	 */
//	@Override
//	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//
//		String userId = TokenManager.getUserId();
//		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//		if (StringUtils.isNotBlank(userId)) {
//			// 根据用户ID查询角色（role），放入到Authorization里。
//			Set<String> roles = iRolesService.findRoleByUserId(userId);
//			info.setRoles(roles);
//			// 根据用户ID查询权限（permission），放入到Authorization里。
//			Set<String> permissions = iOperationsService.findPermissionByUserId(userId);
//			info.setStringPermissions(permissions);
//		} else {
//			SecurityUtils.getSubject().logout();
//		}
//		return info;
//	}
//
//	/**
//	 * 清空当前用户权限信息
//	 */
//	public void clearCachedAuthorizationInfo() {
//		PrincipalCollection principalCollection = SecurityUtils.getSubject().getPrincipals();
//		SimplePrincipalCollection principals = new SimplePrincipalCollection(principalCollection, getName());
//		super.clearCachedAuthorizationInfo(principals);
//	}
//
//	/**
//	 * 指定principalCollection 清除
//	 */
//	public void clearCachedAuthorizationInfo(PrincipalCollection principalCollection) {
//		SimplePrincipalCollection principals = new SimplePrincipalCollection(principalCollection, getName());
//		super.clearCachedAuthorizationInfo(principals);
//	}
//}